package com.android.job.service.request;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import android.os.Handler;
import android.util.Log;

import com.android.job.fusion.FusionCode;
import com.android.job.model.Job;
import com.android.job.service.JSONRequest;
import com.android.job.util.CommonUtil;
import com.android.job.util.json.JSONArray;
import com.android.job.util.json.JSONException;
import com.android.job.util.json.JSONObject;

/**
 * 搜索职位请求
 * 
 * @author
 * 
 */
public class SearchJobRequest extends JSONRequest
{

	private String salary;
	private String region;
	private String type;
	private String degree;
	private String publish;
	private String begin;

	public SearchJobRequest(Handler handler, String httpUrl)
	{
		super(handler, httpUrl);
		// TODO Auto-generated constructor stub
	}

	public void setValues(String salary, String region, String type,
			String degree, String publish, String begin)
	{
		this.salary = salary;
		this.region = region;
		this.type = type;
		this.degree = degree;
		this.publish = publish;
		this.begin = begin;
	}

	@Override
	protected boolean parseJSONResponse(JSONObject json)
	{
		Log.i("SearchJobRequest", "===response===json======" + json.toString());
		if (!json.isNull("jsonData"))
		{
			JSONArray jsonArray;
			try
			{
				jsonArray = json.getJSONArray("jsonData");
				int i = 0;
				List<Job> jobList = new ArrayList<Job>();
				while (!jsonArray.isNull(i))
				{
					JSONObject jsonJob = jsonArray.getJSONObject(i);
					Job job = new Job();
					job.setId(jsonJob.getString("id"));
					job.setComId(jsonJob.getString("comId"));
					job.setCompanyName(jsonJob.getString("companyName"));
					job.setIndustry(jsonJob.getString("industry"));
					job.setNature(jsonJob.getString("nature"));
					job.setPublicTime(jsonJob.getString("publicTime"));
					job.setTypeName(jsonJob.getString("typeName"));
					job.setPostName(jsonJob.getString("postName"));
					job.setNumber(jsonJob.getString("number"));
					job.setSalary(jsonJob.getString("salary"));
					job.setLodging(jsonJob.getString("lodging"));
					job.setWorkExp(jsonJob.getString("workExp"));
					job.setMailingAddress(jsonJob.getString("mailingAddress"));
					job.setLanguage(jsonJob.getString("language"));
					job.setDegree(jsonJob.getString("degree"));
					// job.setOther(jsonJob.getString("other"));
					 job.setContact(jsonJob.getString("contact"));
					 job.setTel(jsonJob.getString("tel"));
					job.setMobile(jsonJob.getString("mobile"));
					// job.setPhoneNumber(jsonJob.getString("phoneNumber"));
//					job.setMobileNumber(jsonJob.getString("mobileNumber"));
					job.setSex(jsonJob.getString("sex"));
					job.setAges(jsonJob.getString("ages"));
					job.setJobNote(jsonJob.getString("jobNote"));
					job.setViewTime(jsonJob.getString("viewTime"));
					job.setPublishType(jsonJob.getString("publishType"));
					job.setWorkerGrade(jsonJob.getString("workerGrade"));
					job.setTechnicalGrade(jsonJob.getString("technicalGrade"));
					job.setMajor(jsonJob.getString("major"));
					job.setWorkConditions(jsonJob.getString("workConditions"));
					job.setWorkPlace(jsonJob.getString("workPlace"));
					job.setPolitics(jsonJob.getString("politics"));
					// job.setForeignLng(jsonJob.getString("foreignLng"));
					// job.setForeignLevel(jsonJob.getString("foreignLevel"));
					job.setComputerLevel(jsonJob.getString("computerLevel"));
					job.setHousingFund(jsonJob.getString("housingFund"));
					job.setRequirements(jsonJob.getString("requirements"));
					job.setJobType(jsonJob.getString("typeName"));
					jobList.add(job);
					i++;
				}
				CommonUtil.sendMessage(handler, FusionCode.RETURN_JSONOBJECT,
						jobList);
			}
			catch (JSONException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		}
		return true;
	}

	@Override
	protected void appendMainBody()
	{
		this.paramsters = new ArrayList();
		try
		{
			jsonObject = new JSONObject();
			jsonObject.put("requestType", "searchPost");
			JSONObject userJson = new JSONObject();
			userJson.put("salary", salary);
			userJson.put("region", region);
			userJson.put("type", type);
			userJson.put("degree", degree);
			userJson.put("publishTime", publish);
			userJson.put("begin", begin);
			jsonObject.put("jsonData", userJson);
			paramsters.add(new BasicNameValuePair("jsonData", jsonObject
					.toString()));
			Log.i("SearchJobRequest",
					"===request===json======" + jsonObject.toString());
		}
		catch (JSONException e)
		{
			e.printStackTrace();
		}
	}

}
